import axios from 'axios'
import Qs from 'qs'

let Axios = axios.create({
    withCredentials: true
})

// debugger
// 请求拦截
Axios.interceptors.request.use(function (config) {
    //post请求
    if (config.method === 'post') {
        if (config.isFormData) {
            config.headers["Content-Type"] = 'application/x-www-form-urlencoded;charset=UTF-8'
            config.data = Qs.stringify(config.data);
        } else {
            config.headers["Content-Type"] = 'application/json;charset=UTF-8'
        }
    }

    //get请求
    if (config.method === 'get') {
        if (config.isFormData) {
            config.headers["Content-Type"] = 'application/x-www-form-urlencoded;charset=UTF-8'
        }
        config.data = Qs.stringify(config.data);
        config.url = config.url + '?' + config.data
    }

    let token = window.localStorage.getItem('token')
    if (token) {
        config.headers['token'] = token;
    }
    return config;
}, function (error) {
    return Promise.reject(error);
});



export default Axios
